/**
 * 
 */
package jvs.peer.net;

import java.net.SocketAddress;

/**
 * @author qiangli
 *
 */
public class PeerAddress extends SocketAddress {

	private static final long serialVersionUID = 1L;

	public static class Realm {
		public static final int XMPP = 0;
		public static final int GTALK = 1;
		public static final int YMSG = 2;
		public static final int MSN = 3;
		public static final int AIM = 4;
		public static final int QQ = 5;
	}
	
	public static final String[] REALMS = {
		"xmpp", "gtalk", "ymsg", "msn", "aim", "qq"
	};
	
	private int realm = Realm.XMPP;
	
	private String id = null; //e.g jid bare address
	
	private String resource = null;
	
	private PeerAddress() {
	}
	
	public PeerAddress(String jid) {
		this();
		this.realm = Realm.XMPP;
		int idx = jid.lastIndexOf("/");
		if (idx > 0) {
			this.id = jid.substring(0,idx);
			this.resource = jid.substring(idx+1);
		} else {
			this.id = jid;
			this.resource = null;
		}
	}
	
	public PeerAddress(String id, String resource) {
		this();
		this.realm = Realm.XMPP;
		this.id = id;
		this.resource = resource;
	}
	
	public PeerAddress(int realm, String id, String resource) {
		this();
		this.realm = realm;
		this.id = id;
		this.resource = resource;
	}

	public int getRealm() {
		return realm;
	}

	public void setRealm(int realm) {
		this.realm = realm;
	}

	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getResource() {
		return resource;
	}


	public void setResource(String resource) {
		this.resource = resource;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + realm;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		PeerAddress other = (PeerAddress) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (realm != other.realm) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "PeerAddress [realm=" + realm + ", id=" + id + ", resource="
				+ resource + "]";
	}
}
